<?php
require_once('DbSql.class.php');
class CommonCrud extends DbSql {

    private $_tbname;
	private $_tbpkey;
    //function __construct($tbname , $tbpkey) {
    function __construct() {
        parent::__construct();
        //$this->_tbname = $tbname;
        //$this->_tbpkey = $tbpkey;
    }
    
	/**
	 * 功能：查询方法，获取数据列表
	 * 返回：二维数组或false
	 */
	public function selectList($tbname, $where='' , $order='' ,  $limit='') {
		$sql = "SELECT * FROM $tbname";
		if($where) {
			$sql .= ' WHERE ' . $where;
		}
		if($order) {
			$sql .= ' ORDER BY ' . $order;
		}
		if($limit) {
			$sql .= ' LIMIT ' . $limit;
		}
		//echo $sql;
		return $this->select($sql);

        //return parent::select($sql);
	}

	//返回一条数据
	public function getOne($tbname,$where='')
	{
		$array = $this->selectList($tbname,$where);
		if($array){
			return $array[0];
		}else{
			return false;
		}
	}

    public function selectkey($id) {
		$sql = "SELECT * FROM $this->_tbname WHERE $this->_tbpkey = "."'$id'";
       // echo $sql;die();
		return $this->select($sql);
	}

    public function selectorder() {
		$sql = "SELECT * FROM $this->_tbname order by $this->_tbpkey desc";
		return $this->select($sql);
	}




	/**
	 * 功能：根据id查询某一条数据的详细信息
	 * 返回：一维数组或false
	 */
	public function selectInfo($id) {
		$sql = "SELECT * FROM $this->_tbname WHERE $this->_tbpkey = "."'$id'";
		$rs = $this->select($sql);
        //echo $sql;die;
		if(!$rs) return false;
		else return $rs[0];
	}


	/**
	 * 功能：查询数据个数
	 * 返回：整数或false
	 */
	public function selectCount($table,$where='') {
		$sql = "SELECT COUNT(*) FROM $table";
		if($where){
			$sql .= ' WHERE ' . $where;
		}
		//echo $sql;
		$rs = parent::select($sql);
       // var_dump($rs);die;
		if(!$rs) return false;
		else return $rs[0][0];
	}


	/**
	 * 功能：根据id更新某一条数据
	 * 返回：true或false
	 * 参数：$data为数组，数组下标为数据库的表中相应的字段
	 */
	function update($table,$where, $data) {
			foreach($data as $key=>$value) {
				@$values .= '`'.$key.'`' . "='" . $value . "'" . ',';
			}

			$values = substr($values , 0 , -1);
            
			$sql = "UPDATE $table SET ". $values . " WHERE $where";

          	//echo $sql;

			parent::execute($sql);
			return mysql_affected_rows();
		}


	/**
	 * 功能：插入一条新数据
	 * 返回：true或false
	 * 参数：$data为数组，数组下标为数据库的表中相应的字段
	 */
        function insert($table,$data) {
           $fields='';
           $values='';
			foreach($data as $key=>$value) {
				$fields .='`'.$key.'`' . ',';
				$values .= "'" . $value . "'" . ',';
			}
			$fields = substr($fields , 0 , -1);
			$values = substr($values , 0 , -1);

			$sql = "INSERT INTO $table($fields) VALUES($values)";
            //echo $sql;die;
            
			parent::execute($sql);
			return mysql_insert_id();

		}


	/**
	 * 功能：根据ID删除数据
	 * 返回：true或false
	 */
	public function delete($table,$where) {
		$sql = "DELETE FROM $table  WHERE  $where";
       //echo $sql;die;
		parent::execute($sql);
		return mysql_affected_rows();

	}
    public function deleteBatch($ids) {
		$sql = "DELETE FROM $this->_tbname  WHERE  $this->_tbpkey in". '('.$ids.')';
        //echo $sql;die;
		return $this->execute($sql);

	}


    public function Seartch($keywords,$extent) {

            foreach($extent as $key => $value)
            {

                if($key == 0)
                {
                    $Seartchkey .= ' where ' . $value . ' like ' . "'%$keywords%'";
                }
                else
                {
                    $Seartchkey .= ' or ' . $value . ' like ' . "'%$keywords%'" ;
                }


            }
			$sql = 'select * from  ' . $this->_tbname  .  $Seartchkey;
			//echo $sql;die;
			return parent::select($sql);
	}
}

